﻿using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff
{
    struct RangeAddress16
    {
        private ushort firstRow;
        private ushort lastRow;
        private ushort firstColumn;
        private ushort lastColumn;

        public RangeAddress16(BinaryReader reader)
        {
            this.firstRow = reader.ReadUInt16();
            this.lastRow = reader.ReadUInt16();
            this.firstColumn = reader.ReadUInt16();
            this.lastColumn = reader.ReadUInt16();
        }

        public static explicit operator RangeAddress(RangeAddress16 ra)
        {
            return new RangeAddress(new CellAddress(ra.firstRow, ra.firstColumn), new CellAddress(ra.lastRow, ra.lastColumn));

        }
        public static explicit operator RangeAddress16(RangeAddress ra)
        {
            return new RangeAddress16(){firstRow =  (ushort) ra.TopLeft.Row, firstColumn = (ushort) ra.TopLeft.Column, lastColumn =(ushort) ra.BottomRight.Column, lastRow = (ushort) ra.BottomRight.Row};

        }


        public void Read (BinaryReader reader)
        {
            this.firstRow = reader.ReadUInt16();
            this.lastRow = reader.ReadUInt16();
            this. firstColumn = reader.ReadUInt16();
            this.lastColumn = reader.ReadUInt16();

        }

        public void Write(BinaryWriter writer)
        {
            writer.Write(firstRow);
            writer.Write(lastRow);
            writer.Write(firstColumn);
            writer.Write(lastColumn);
        }
    }
}